<template>
	<div>
		<el-row style="padding-top: 10px">
			<el-row style="margin-bottom: 10px;">
				<el-col :span="12" style="display: flex;gap: 10px;">
					<div>开帐年份：<el-date-picker v-model="SettlementNumber" type="year"  value-format="yyyy" placeholder="选择年份" @change="SettlementNumberEvent"></el-date-picker></div>
					<div style="display: flex;align-items: center;"><span style="width: 150px;">统一社保金额：</span><el-input v-model="mainsocial" @blur="mainsocialEvent"></el-input></div>
				</el-col>
				<el-col :span="12" align="right">
					<el-button type="primary" @click="AddSocialinsuranceHandler">添加社保</el-button>
				</el-col>
			</el-row>
		    <el-col>
		        <el-table :data="YgUserList" style="width: 100%;"  ref="multipleTable"  border :row-style="{ height: '20px' }"
		            :cell-style="{ padding: '5px 0 ' }">
		            <!-- <el-table-column header-align="center" type="selection" width="50" align="center"></el-table-column> -->
		            <el-table-column header-align="center" fixed type="index" label="序号" width="50" align="center"
		                :index="_indexMethod"></el-table-column>
		            <el-table-column header-align="center" fixed prop="userName" label="姓名" align="center">
		            </el-table-column>
		            <el-table-column header-align="center" prop="userNumber" label="工号" align="center">
		            </el-table-column>
		            <el-table-column header-align="center" prop="sex" label="性别" align="center">
		                <template slot-scope="scope">
		                    <label v-if="scope.row.sex == 0">男</label>
		                    <label v-else>女</label>
		                </template>
		            </el-table-column>
<!-- 		            <el-table-column header-align="center" prop="RoleId" label="岗位" align="center"
		                :show-overflow-tooltip="true">
		                <template slot-scope="scope">
		                    {{ RoleNamesByids(scope.row.RoleId) }}
		                </template>
		            </el-table-column>
		            <el-table-column header-align="center" prop="Card" label="身份证号" align="center"
		                width="180"></el-table-column>
		            <el-table-column header-align="center" prop="Phone" label="联系电话" align="center"
		                width="150"></el-table-column>
		            <el-table-column header-align="center" prop="NowFamilyAddress" label="现居地" align="center"
		                minWidth='330' :show-overflow-tooltip="true"></el-table-column>
		            <el-table-column header-align="center" prop="FamilyAddress" label="户籍" align="center" minWidth='330'
		                :show-overflow-tooltip="true">
					</el-table-column>
		            <el-table-column header-align="center" prop="JobNumber" label="工种" align="center"
		                width="110"></el-table-column>
		            <el-table-column header-align="center" prop="InductionTime" label="入职时间" align="center" width="110">
		                <template slot-scope="scope">
		                    {{ _fmtDateGetStr(scope.row.InductionTime, "") }}
		                </template>
		            </el-table-column>
		            <el-table-column header-align="center" prop="DepartureTime" label="离职日期" align="center" width="110">
		                <template slot-scope="scope">
		                    {{ _fmtDateGetStr(scope.row.DepartureTime, "暂未离职") }}
		                </template>
		            </el-table-column>
		
		            <el-table-column header-align="center" prop="BankCardNumber" label="银行卡号" align="center"
		                minWidth='110' :show-overflow-tooltip="true"></el-table-column>
		            <el-table-column header-align="center" prop="BankName" label="开户行" align="center" width="110"
		                :show-overflow-tooltip="true"></el-table-column>
		            <el-table-column header-align="center" prop="InternshipState" label="状态" align="center" width="110">
		                <template slot-scope="scope">
		                    {{ getEmployState(scope.row.InternshipState) }}
		                </template>
		            </el-table-column> -->
					<el-table-column header-align="center" prop="Card" label="社保" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.socialinsurance"></el-input>
						</template>
					</el-table-column>
		        </el-table>
		    </el-col>
		</el-row>
	</div>
</template>

<script>
	import * as publicjs from "/public/Scriput/Public"
	import dayjs from "dayjs"
	export default {
		name:'ProjectManagement',
		props:{
			dialogshow:{
				type:Boolean,
				required:true
			}
		},
		data(){
			return{
				YgUserList:[],
				UserIds:'', 
				SettlementNumber: dayjs(Date()).format('YYYY'),
				mainsocial:'',
				isUpdata:false,
			}
		},
		watch:{
			dialogshow:{
				handler(val){
					this.getYGUserLis()
				},
				immediate:true
			}
		},
		created() {
			publicjs.http(this.$http, this.$message)
		},
		mounted() {
			this.getYGUserLis()
			this.AllRoleDB = publicjs.GetAllRole(this.$http);
		},
		methods:{
			_indexMethod(index) {
			    return index + 1
			},
			//获取所有人员(员工)
			getYGUserLis(){
				this.$http.post('FinanceManagement/SelFinanceManagement', {
					userId: '',
					SettlementNumber:this.SettlementNumber,
					pageSize: 1000,
					pageIndex: 1,
				}).then(res=>{
					const { StatusCode,Data } = res.data || 0
					if(StatusCode == 200){
						this.isUpdata = Data.filter(x=>x.SocialInsurance!=0 && x.SocialInsurance!='').length>0?true:false
						this.YgUserList = Data.map(x=>{
							let namelis = x.UserName.split('-')
							return {
								id: 0,
								delete: false,
								operatId: 0,
								userId: x.UserId,
								userName: namelis[0],
								userNumber: namelis[1], 
								settlementNumber: this.SettlementNumber,
								socialinsurance: x.SocialInsurance
							}
						})
					} else this.YgUserList = []
				})		
				// this.$http.post('UserInfo/GetEmployeeUsers').then(res=>{
				// 	const { StatusCode,Data } = res.data || 0
				// 	if(StatusCode == 200){
				// 		this.YgUserList = Data.map(x=>{
				// 			return {
				// 				id: 0,
				// 				delete: false,
				// 				operatId: 0,
				// 				userId: x.Id,
				// 				userName: x.UserName,
				// 				userNumber: x.UserNumber, 
				// 				sex: x.Sex,
				// 				settlementNumber: this.SettlementNumber,
				// 				socialinsurance: 0
				// 			}
				// 		})
				// 	} 
				// 	else this.YgUserList = []
				// })
			},
			//权限名称
			RoleNamesByids(ids) {
			    if (ids != "") {
			        let rolenames = [];
			        let idsstr = ids;
			        let roleids = idsstr.split(',');
			        let _this = this;
			        roleids.forEach(function (item, index) {
			            //item 就是当日按循环到的对象
			            //index是循环的索引，从0开始
			            let iid = parseInt(item);
			            var role = _this.AllRoleDB.find((x) => x.Id == iid);
			            if (role != null) {
			                rolenames.push(role.RoleName);
			            }
			        })
			        return rolenames.join(',');
			    }
			},
			_fmtDateGetStr(obj, str) {
			    if (obj == null || obj == "") {
			        return str;
			    } else {
			        return this.fmtDate(obj);
			    }
			},
			//转换时间戳为年月日,20210108修复负数时间戳的转换问题
			fmtDate(obj) {
			    //var result = "";
			    //if (obj != null || obj != undefined) {
			    //    var data = parseInt(new RegExp(/\d+/).exec(obj)[0]);
			    //    if (obj.indexOf("-") > 0)
			    //        data = -data;
			    //    var date = new Date(data);
			    //    var y = 1900 + date.getYear();
			    //    var m = "0" + (date.getMonth() + 1);
			    //    var d = "0" + date.getDate();
			    //    if (y > 1920 && y < 2099)
			    //        result = y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length);
			    //}			
			    return obj.split(' ')[0];
			},
			getEmployState(obj) {
			    var tate;
			    if (obj == 1)
			        tate = "试用期";
			    else if (obj == 2)
			        tate = "实习期";
			    else if (obj == 4)
			        tate = "正式期";
			    else if (obj == 8)
			        tate = "离职";
			    else if (obj == 16)
			        tate = "兼职";
			    else
			        tate = "未选择";
			    return tate;
			},
			SettlementNumberEvent(value){
				this.YgUserList.forEach(x=>{
					x.settlementNumber = value
				})
				this.getYGUserLis()
			},
			mainsocialEvent(){
				this.YgUserList.forEach(x=>{
					x.socialinsurance = this.mainsocial
				})
			},
			//添加年度社保
			AddSocialinsuranceHandler(){
				let erroData = this.YgUserList.filter(x=>(x.socialinsurance !=0 || x.socialinsurance!=''))
				if(erroData.length == 0) {
					this.$message.warning('请填写人员社保数据!')
					return
				}
				let url
				if(this.isUpdata) url = 'Socialinsurance/UpSocialinsurance'					
				else url = 'Socialinsurance/AddSocialinsurance'													
				this.$http.post( url,{
					socialinsurancetList: this.YgUserList
				}).then(res=>{
					const {
						StatusCode,
						Data,
						Message
					} = res.data || 0
					if (StatusCode == 200) {
						if(this.isUpdata) this.$message.success('编辑成功')
						else this.$message.success('添加成功')
						this.$parent.$parent.PersonnelSocialSecurityShow = false
						this.$parent.$parent.getTableData()
					} else {
						this.$message.warning(Message)
					}
				})
			}
		}
	}
</script>

<style scoped>
</style>